import { _decorator, Component, Graphics, math, Node, v2, v3, Vec2, Vec3 } from 'cc';
import { Draw2D } from '../Common/utils/Draw2D';
import { VecUtils } from '../Common/utils/VecUtils';
const { ccclass, property } = _decorator;

@ccclass('TestReflection')
export class TestReflection extends Component {

    @property(Graphics)
    gps: Graphics = null

    @property(Node)
    pointA: Node = null

    @property(Node)
    pointB: Node = null

    @property(Node)
    pointCenter: Node = null

    @property(Node)
    testNode: Node = null


    private _draw2D: Draw2D


    start() {
        this._draw2D = new Draw2D(this.gps)
    
    }

    update(deltaTime: number) {
        this._draw2D.clear()
        this._draw2D.line(this.pointCenter.position.toVec2(), this.pointA.position.toVec2())
        this._draw2D.line(this.pointCenter.position.toVec2(), this.pointB.position.toVec2())

        const inicidentDirection = v3()
        Vec3.subtract(inicidentDirection, this.pointCenter.position, this.pointA.position) 
        const inicidentLength = inicidentDirection.length()
        inicidentDirection.normalize()

        const normalDirection = v3()
        Vec3.subtract(normalDirection, this.pointB.position, this.pointCenter.position) 
        normalDirection.normalize()

        // console.log('inicidentDirection:', inicidentDirection, 'normalDirection:', normalDirection)

        const reflectedDorecton = VecUtils.getReflectionDirection(inicidentDirection.clone(), normalDirection.clone())
        const reflectedPoint = v3();
        Vec3.multiplyScalar(reflectedPoint, reflectedDorecton, inicidentLength)
      
        // console.log("reflectedDorecton", reflectedDorecton)
        this.testNode.setPosition(reflectedPoint)
        this._draw2D.line(this.pointCenter.position.toVec2(), reflectedPoint.toVec2())
    }
}   